package com.wei.controller;

import com.wei.service.PayLogService;
import com.wei.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author wjx
 * @ClassName PayLogController
 * @description: TODO
 * @date 2022-12-04 22:50
 * @version: 1.0
 */
@RestController
@RequestMapping("/eduorder/paylog")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {

        //返回信息，包含二维码地址，和其他信息
        Map<String, Object> map = payLogService.createWxNativeInfo(orderNo);

        return R.ok().data("map", map);
    }

    @GetMapping("/queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {

        //调用查询接口
        Map<String, String> map = payLogService.queryPayStatus(orderNo);

        if (map == null) {
            return R.error().message("支付错误");
        }
        if (map.get("trade_state").equals("SUCCESS")) {
            //更改订单状态
            payLogService.updateOrderStatus(map);

            return R.ok().message("支付成功");
        }

        return R.ok().code(25000).message("支付中");
    }


}